package org.vacoor.nothing.misc.express.fedex;

import com.fedex.country.stub.*;

/**
 * 根据邮编查找附近网点 ????
 */
public class FedExPostalCodeInquiryService extends FedExSupport {

    // 查询邮编对应区域的酷 FedEx 服务点??
    public void postalCodeInquiry() {
        //
        PostalCodeInquiryRequest req = new PostalCodeInquiryRequest();

        // 授权, 客户端, 协议版本信息
        req.setWebAuthenticationDetail(new WebAuthenticationDetail(new WebAuthenticationCredential(getKey(), getPassword())));
        req.setClientDetail(new ClientDetail(getAccountNumber(), getMeterNumber(), getIntegratorId(), null, null, null));
        req.setTransactionDetail(new TransactionDetail("PostalCode Inquiry Request", null));
        req.setVersion(new VersionId("cnty", 2, 0, 0));

        //
        req.setPostalCode("32810");
        req.setCountryCode("US");

        //
        try {
            // Initialize the service
            CountryServiceLocator service;
            CountryPortType port;
            //
            service = new CountryServiceLocator();
            updateEndPoint(service);
            port = service.getCountryServicePort();
            // This is the call to the web service
            PostalCodeInquiryReply reply = port.postalCodeInquiry(req);
            //
            if (isResponseOk(reply.getHighestSeverity())) {
                System.out.println("--- Express Description ---");
                System.out.println("Location id - " + reply.getExpressDescription().getLocationId());
                System.out.println("Postal Code - " + reply.getExpressDescription().getPostalCode());
                System.out.println("Service area - " + reply.getExpressDescription().getServiceArea());
                System.out.println("--- Express Freight Description ---");
                System.out.println("Location id - " + reply.getExpressFreightDescription().getLocationId());
                System.out.println("Postal Code - " + reply.getExpressFreightDescription().getPostalCode());
                System.out.println("Service area - " + reply.getExpressFreightDescription().getServiceArea());
            }
            System.out.println("");
            printNotifications(reply.getNotifications());

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    private static boolean isResponseOk(NotificationSeverityType notificationSeverityType) {
        if (notificationSeverityType == null) {
            return false;
        }
        if (notificationSeverityType.equals(NotificationSeverityType.WARNING) ||
                notificationSeverityType.equals(NotificationSeverityType.NOTE) ||
                notificationSeverityType.equals(NotificationSeverityType.SUCCESS)) {
            return true;
        }
        return false;
    }

    private static void printNotifications(Notification[] notifications) {
        System.out.println("Notifications:");
        if (notifications == null || notifications.length == 0) {
            System.out.println("  No notifications returned");
        }
        for (int i = 0; i < notifications.length; i++) {
            Notification n = notifications[i];
            System.out.print("  Notification no. " + i + ": ");
            if (n == null) {
                System.out.println("null");
                continue;
            } else {
                System.out.println("");
            }
            NotificationSeverityType nst = n.getSeverity();

            System.out.println("    Severity: " + (nst == null ? "null" : nst.getValue()));
            System.out.println("    Code: " + n.getCode());
            System.out.println("    Message: " + n.getMessage());
            System.out.println("    Source: " + n.getSource());
        }
    }

    private static void updateEndPoint(CountryServiceLocator serviceLocator) {
        String endPoint = System.getProperty("endPoint");
        if (endPoint != null) {
            serviceLocator.setCountryServicePortEndpointAddress(endPoint);
        }
    }
}
